package com.uniflow.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 任务中心实体类
 */
@ApiModel(description = "任务中心")
@TableName("task_center")
public class TaskCenter {
    
    @ApiModelProperty("任务ID")
    @TableId(type = IdType.ASSIGN_UUID)
    private String id;
    
    @ApiModelProperty("任务标题")
    private String title;
    
    @ApiModelProperty("任务描述")
    private String description;
    
    @ApiModelProperty("任务类型")
    private String type;
    
    @ApiModelProperty("任务分类")
    private String category;
    
    @ApiModelProperty("优先级")
    private Integer priority;
    
    @ApiModelProperty("任务状态")
    private String status;
    
    @ApiModelProperty("执行人ID")
    private String assigneeId;
    
    @ApiModelProperty("执行人姓名")
    private String assigneeName;
    
    @ApiModelProperty("创建人ID")
    private String createdBy;
    
    @ApiModelProperty("创建人姓名")
    private String createdByName;
    
    @ApiModelProperty("更新人ID")
    private String updatedBy;
    
    @ApiModelProperty("更新人姓名")
    private String updatedByName;
    
    @ApiModelProperty("业务键")
    private String businessKey;
    
    @ApiModelProperty("业务类型")
    private String businessType;
    
    @ApiModelProperty("业务数据JSON")
    private String businessDataJson;
    
    @ApiModelProperty("流程实例ID")
    private String processInstanceId;
    
    @ApiModelProperty("流程定义ID")
    private String processDefinitionId;
    
    @ApiModelProperty("流程定义Key")
    private String processDefinitionKey;
    
    @ApiModelProperty("流程任务ID")
    private String processTaskId;
    
    @ApiModelProperty("表单ID")
    private String formId;
    
    @ApiModelProperty("表单数据ID")
    private String formDataId;
    
    @ApiModelProperty("开始时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime startTime;
    
    @ApiModelProperty("到期时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime dueTime;
    
    @ApiModelProperty("完成时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime completedTime;
    
    @ApiModelProperty("创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;
    
    @ApiModelProperty("更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updatedAt;
    
    @ApiModelProperty("标签")
    private String tags;
    
    @ApiModelProperty("备注")
    private String remark;
    
    @ApiModelProperty("扩展属性JSON")
    private String extendJson;
    
    @ApiModelProperty("排序")
    private Integer sort;
    
    @ApiModelProperty("是否删除")
    private Boolean deleted;
    
    @ApiModelProperty("租户ID")
    private String tenantId;
    
    // 非数据库字段
    
    @ApiModelProperty("业务数据")
    @TableField(exist = false)
    private Map<String, Object> businessData;
    
    @ApiModelProperty("扩展属性")
    @TableField(exist = false)
    private Map<String, Object> extend;
    
    @ApiModelProperty("标签列表")
    @TableField(exist = false)
    private List<String> tagList;
    
    @ApiModelProperty("候选用户列表")
    @TableField(exist = false)
    private List<String> candidateUsers;
    
    @ApiModelProperty("候选组列表")
    @TableField(exist = false)
    private List<String> candidateGroups;
    
    @ApiModelProperty("任务持续时间（毫秒）")
    @TableField(exist = false)
    private Long duration;
    
    @ApiModelProperty("是否过期")
    @TableField(exist = false)
    private Boolean overdue;
    
    @ApiModelProperty("剩余时间（毫秒）")
    @TableField(exist = false)
    private Long remainingTime;
    
    @ApiModelProperty("流程信息")
    @TableField(exist = false)
    private ProcessInfo processInfo;
    
    @ApiModelProperty("表单信息")
    @TableField(exist = false)
    private FormInfo formInfo;
    
    @ApiModelProperty("任务历史")
    @TableField(exist = false)
    private List<TaskHistory> taskHistories;
    
    @ApiModelProperty("任务评论")
    @TableField(exist = false)
    private List<TaskComment> taskComments;
    
    // 内部类定义
    
    /**
     * 流程信息
     */
    public static class ProcessInfo {
        private String processInstanceId;
        private String processDefinitionId;
        private String processDefinitionKey;
        private String processDefinitionName;
        private String processDefinitionVersion;
        private String businessKey;
        private String startUserId;
        private String startUserName;
        private LocalDateTime startTime;
        private LocalDateTime endTime;
        private String status;
        
        // getters and setters
        public String getProcessInstanceId() { return processInstanceId; }
        public void setProcessInstanceId(String processInstanceId) { this.processInstanceId = processInstanceId; }
        public String getProcessDefinitionId() { return processDefinitionId; }
        public void setProcessDefinitionId(String processDefinitionId) { this.processDefinitionId = processDefinitionId; }
        public String getProcessDefinitionKey() { return processDefinitionKey; }
        public void setProcessDefinitionKey(String processDefinitionKey) { this.processDefinitionKey = processDefinitionKey; }
        public String getProcessDefinitionName() { return processDefinitionName; }
        public void setProcessDefinitionName(String processDefinitionName) { this.processDefinitionName = processDefinitionName; }
        public String getProcessDefinitionVersion() { return processDefinitionVersion; }
        public void setProcessDefinitionVersion(String processDefinitionVersion) { this.processDefinitionVersion = processDefinitionVersion; }
        public String getBusinessKey() { return businessKey; }
        public void setBusinessKey(String businessKey) { this.businessKey = businessKey; }
        public String getStartUserId() { return startUserId; }
        public void setStartUserId(String startUserId) { this.startUserId = startUserId; }
        public String getStartUserName() { return startUserName; }
        public void setStartUserName(String startUserName) { this.startUserName = startUserName; }
        public LocalDateTime getStartTime() { return startTime; }
        public void setStartTime(LocalDateTime startTime) { this.startTime = startTime; }
        public LocalDateTime getEndTime() { return endTime; }
        public void setEndTime(LocalDateTime endTime) { this.endTime = endTime; }
        public String getStatus() { return status; }
        public void setStatus(String status) { this.status = status; }
    }
    
    /**
     * 表单信息
     */
    public static class FormInfo {
        private String formId;
        private String formName;
        private String formTitle;
        private String formDataId;
        private String formUrl;
        
        // getters and setters
        public String getFormId() { return formId; }
        public void setFormId(String formId) { this.formId = formId; }
        public String getFormName() { return formName; }
        public void setFormName(String formName) { this.formName = formName; }
        public String getFormTitle() { return formTitle; }
        public void setFormTitle(String formTitle) { this.formTitle = formTitle; }
        public String getFormDataId() { return formDataId; }
        public void setFormDataId(String formDataId) { this.formDataId = formDataId; }
        public String getFormUrl() { return formUrl; }
        public void setFormUrl(String formUrl) { this.formUrl = formUrl; }
    }
    
    /**
     * 任务历史
     */
    public static class TaskHistory {
        private String id;
        private String taskId;
        private String action;
        private String actionName;
        private String fromStatus;
        private String toStatus;
        private String operatorId;
        private String operatorName;
        private String comment;
        private LocalDateTime operatedAt;
        
        // getters and setters
        public String getId() { return id; }
        public void setId(String id) { this.id = id; }
        public String getTaskId() { return taskId; }
        public void setTaskId(String taskId) { this.taskId = taskId; }
        public String getAction() { return action; }
        public void setAction(String action) { this.action = action; }
        public String getActionName() { return actionName; }
        public void setActionName(String actionName) { this.actionName = actionName; }
        public String getFromStatus() { return fromStatus; }
        public void setFromStatus(String fromStatus) { this.fromStatus = fromStatus; }
        public String getToStatus() { return toStatus; }
        public void setToStatus(String toStatus) { this.toStatus = toStatus; }
        public String getOperatorId() { return operatorId; }
        public void setOperatorId(String operatorId) { this.operatorId = operatorId; }
        public String getOperatorName() { return operatorName; }
        public void setOperatorName(String operatorName) { this.operatorName = operatorName; }
        public String getComment() { return comment; }
        public void setComment(String comment) { this.comment = comment; }
        public LocalDateTime getOperatedAt() { return operatedAt; }
        public void setOperatedAt(LocalDateTime operatedAt) { this.operatedAt = operatedAt; }
    }
    
    /**
     * 任务评论
     */
    public static class TaskComment {
        private String id;
        private String taskId;
        private String content;
        private String commenterId;
        private String commenterName;
        private LocalDateTime commentedAt;
        
        // getters and setters
        public String getId() { return id; }
        public void setId(String id) { this.id = id; }
        public String getTaskId() { return taskId; }
        public void setTaskId(String taskId) { this.taskId = taskId; }
        public String getContent() { return content; }
        public void setContent(String content) { this.content = content; }
        public String getCommenterId() { return commenterId; }
        public void setCommenterId(String commenterId) { this.commenterId = commenterId; }
        public String getCommenterName() { return commenterName; }
        public void setCommenterName(String commenterName) { this.commenterName = commenterName; }
        public LocalDateTime getCommentedAt() { return commentedAt; }
        public void setCommentedAt(LocalDateTime commentedAt) { this.commentedAt = commentedAt; }
    }
    
    // 业务方法
    
    /**
     * 判断任务是否已分配
     */
    public boolean isAssigned() {
        return assigneeId != null && !assigneeId.trim().isEmpty();
    }
    
    /**
     * 判断任务是否过期
     */
    public boolean isOverdue() {
        if (dueTime == null || completedTime != null) {
            return false;
        }
        return LocalDateTime.now().isAfter(dueTime);
    }
    
    /**
     * 判断任务是否已完成
     */
    public boolean isCompleted() {
        return "completed".equals(status) || completedTime != null;
    }
    
    /**
     * 获取任务持续时间（毫秒）
     */
    public Long getDuration() {
        if (startTime == null) {
            return null;
        }
        LocalDateTime endTime = completedTime != null ? completedTime : LocalDateTime.now();
        return java.time.Duration.between(startTime, endTime).toMillis();
    }
    
    /**
     * 获取剩余时间（毫秒）
     */
    public Long getRemainingTime() {
        if (dueTime == null || isCompleted()) {
            return null;
        }
        LocalDateTime now = LocalDateTime.now();
        if (now.isAfter(dueTime)) {
            return 0L;
        }
        return java.time.Duration.between(now, dueTime).toMillis();
    }
    
    /**
     * 获取优先级描述
     */
    public String getPriorityDescription() {
        if (priority == null) {
            return "普通";
        }
        switch (priority) {
            case 1: return "低";
            case 2: return "普通";
            case 3: return "高";
            case 4: return "紧急";
            case 5: return "非常紧急";
            default: return "普通";
        }
    }
    
    /**
     * 获取状态描述
     */
    public String getStatusDescription() {
        if (status == null) {
            return "未知";
        }
        switch (status) {
            case "created": return "已创建";
            case "assigned": return "已分配";
            case "in_progress": return "进行中";
            case "suspended": return "已暂停";
            case "completed": return "已完成";
            case "cancelled": return "已取消";
            case "rejected": return "已拒绝";
            default: return status;
        }
    }
    
    /**
     * 格式化持续时间
     */
    public String getFormattedDuration() {
        Long duration = getDuration();
        if (duration == null) {
            return "";
        }
        
        long seconds = duration / 1000;
        long minutes = seconds / 60;
        long hours = minutes / 60;
        long days = hours / 24;
        
        if (days > 0) {
            return days + "天" + (hours % 24) + "小时";
        } else if (hours > 0) {
            return hours + "小时" + (minutes % 60) + "分钟";
        } else if (minutes > 0) {
            return minutes + "分钟";
        } else {
            return seconds + "秒";
        }
    }
    
    // getters and setters
    
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }
    public String getDescription() { return description; }
    public void setDescription(String description) { this.description = description; }
    public String getType() { return type; }
    public void setType(String type) { this.type = type; }
    public String getCategory() { return category; }
    public void setCategory(String category) { this.category = category; }
    public Integer getPriority() { return priority; }
    public void setPriority(Integer priority) { this.priority = priority; }
    public String getStatus() { return status; }
    public void setStatus(String status) { this.status = status; }
    public String getAssigneeId() { return assigneeId; }
    public void setAssigneeId(String assigneeId) { this.assigneeId = assigneeId; }
    public String getAssigneeName() { return assigneeName; }
    public void setAssigneeName(String assigneeName) { this.assigneeName = assigneeName; }
    public String getCreatedBy() { return createdBy; }
    public void setCreatedBy(String createdBy) { this.createdBy = createdBy; }
    public String getCreatedByName() { return createdByName; }
    public void setCreatedByName(String createdByName) { this.createdByName = createdByName; }
    public String getUpdatedBy() { return updatedBy; }
    public void setUpdatedBy(String updatedBy) { this.updatedBy = updatedBy; }
    public String getUpdatedByName() { return updatedByName; }
    public void setUpdatedByName(String updatedByName) { this.updatedByName = updatedByName; }
    public String getBusinessKey() { return businessKey; }
    public void setBusinessKey(String businessKey) { this.businessKey = businessKey; }
    public String getBusinessType() { return businessType; }
    public void setBusinessType(String businessType) { this.businessType = businessType; }
    public String getBusinessDataJson() { return businessDataJson; }
    public void setBusinessDataJson(String businessDataJson) { this.businessDataJson = businessDataJson; }
    public String getProcessInstanceId() { return processInstanceId; }
    public void setProcessInstanceId(String processInstanceId) { this.processInstanceId = processInstanceId; }
    public String getProcessDefinitionId() { return processDefinitionId; }
    public void setProcessDefinitionId(String processDefinitionId) { this.processDefinitionId = processDefinitionId; }
    public String getProcessDefinitionKey() { return processDefinitionKey; }
    public void setProcessDefinitionKey(String processDefinitionKey) { this.processDefinitionKey = processDefinitionKey; }
    public String getProcessTaskId() { return processTaskId; }
    public void setProcessTaskId(String processTaskId) { this.processTaskId = processTaskId; }
    public String getFormId() { return formId; }
    public void setFormId(String formId) { this.formId = formId; }
    public String getFormDataId() { return formDataId; }
    public void setFormDataId(String formDataId) { this.formDataId = formDataId; }
    public LocalDateTime getStartTime() { return startTime; }
    public void setStartTime(LocalDateTime startTime) { this.startTime = startTime; }
    public LocalDateTime getDueTime() { return dueTime; }
    public void setDueTime(LocalDateTime dueTime) { this.dueTime = dueTime; }
    public LocalDateTime getCompletedTime() { return completedTime; }
    public void setCompletedTime(LocalDateTime completedTime) { this.completedTime = completedTime; }
    public LocalDateTime getCreatedAt() { return createdAt; }
    public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }
    public LocalDateTime getUpdatedAt() { return updatedAt; }
    public void setUpdatedAt(LocalDateTime updatedAt) { this.updatedAt = updatedAt; }
    public String getTags() { return tags; }
    public void setTags(String tags) { this.tags = tags; }
    public String getRemark() { return remark; }
    public void setRemark(String remark) { this.remark = remark; }
    public String getExtendJson() { return extendJson; }
    public void setExtendJson(String extendJson) { this.extendJson = extendJson; }
    public Integer getSort() { return sort; }
    public void setSort(Integer sort) { this.sort = sort; }
    public Boolean getDeleted() { return deleted; }
    public void setDeleted(Boolean deleted) { this.deleted = deleted; }
    public String getTenantId() { return tenantId; }
    public void setTenantId(String tenantId) { this.tenantId = tenantId; }
    public Map<String, Object> getBusinessData() { return businessData; }
    public void setBusinessData(Map<String, Object> businessData) { this.businessData = businessData; }
    public Map<String, Object> getExtend() { return extend; }
    public void setExtend(Map<String, Object> extend) { this.extend = extend; }
    public List<String> getTagList() { return tagList; }
    public void setTagList(List<String> tagList) { this.tagList = tagList; }
    public List<String> getCandidateUsers() { return candidateUsers; }
    public void setCandidateUsers(List<String> candidateUsers) { this.candidateUsers = candidateUsers; }
    public List<String> getCandidateGroups() { return candidateGroups; }
    public void setCandidateGroups(List<String> candidateGroups) { this.candidateGroups = candidateGroups; }
    public void setDuration(Long duration) { this.duration = duration; }
    public Boolean getOverdue() { return overdue; }
    public void setOverdue(Boolean overdue) { this.overdue = overdue; }
    public void setRemainingTime(Long remainingTime) { this.remainingTime = remainingTime; }
    public ProcessInfo getProcessInfo() { return processInfo; }
    public void setProcessInfo(ProcessInfo processInfo) { this.processInfo = processInfo; }
    public FormInfo getFormInfo() { return formInfo; }
    public void setFormInfo(FormInfo formInfo) { this.formInfo = formInfo; }
    public List<TaskHistory> getTaskHistories() { return taskHistories; }
    public void setTaskHistories(List<TaskHistory> taskHistories) { this.taskHistories = taskHistories; }
    public List<TaskComment> getTaskComments() { return taskComments; }
    public void setTaskComments(List<TaskComment> taskComments) { this.taskComments = taskComments; }
}